#ifndef _SENSOR_SENSOR_APP_H_
#define _SENSOR_SENSOR_APP_H_

#include "adc.h"
#include "SENSOR/adc_app.h"
#include "SENSOR/DS18B20.h"
#include "SENSOR/ultrasonic.h"
#include "NRF24/NRF24_app.h"

/**
 ****************************************************************************************************
 * DONE 用户变量区
 ****************************************************************************************************
 */
	
typedef struct {
    uint16_t adc_new[8];         // 当前ADC值
    uint16_t adc_raw[8];         // 原始ADC值（8个通道）
    uint16_t adc_filtered[8];    // 滤波后的ADC值
    uint16_t voltage[8];         // 转换后的电压值（单位：mV）
    double adc_map[8];         // 映射后的值
} SensorData;


//-----------------------------------------------	

//传感器数据映射模式
#define SENSOR_MAP_MODE  2
//ADC读取模式
#define ADC_READ_MODE 2

#define ADC_CHANNEL_NUM 3

#define DATA_COUNT  ADC1_CHANNEL_CNT  

#define MAX_STR_LEN  12

#define TDS_READ_TIMES 5
#define TS_K 2047.19

#define Water_depth_MAX 1.0f
//-----------------------------------------------	


extern SensorData ADC_Data;

extern double temperature;

extern float distance;

extern double water_sample;


/**
 ****************************************************************************************************
 * DONE 用户函数区
 ****************************************************************************************************
 */

/**
 * @if Eng 
 * @brief Measures and updates distance from distance sensor
 * @param None 
 * @result Distance measurement is stored in sensor data structure 
 * @else 
 * @brief 测量并更新距离传感器的距离 
 * @param 无 
 * @result 距离测量值存储在传感器数据结构中 
 * @endif 
 */
void sensor_Distance(void);

/**
 * @if Eng
 * @brief Measures and updates temperature from temperature sensor 
 * @param None
 * @result Temperature measurement is stored in sensor data structure
 * @else
 * @brief 测量并更新温度传感器的温度 
 * @param 无 
 * @result 温度测量值存储在传感器数据结构中 
 * @endif 
 */
void sensor_GetTemperature(void);

/**
 * @if Eng 
 * @brief Initializes all sensor hardware
 * @param None 
 * @result All sensors are initialized and ready for measurement 
 * @else 
 * @brief 初始化所有传感器硬件
 * @param 无
 * @result 所有传感器初始化完成并准备测量
 * @endif 
 */
void sensor_Init(void);

#if ADC_READ_MODE == 0

/**
 * @if Eng 
 * @brief Reads and processes all ADC sensors (polling mode)
 * @param hadc - Pointer to ADC handle structure
 * @result All sensor data is updated with latest ADC readings
 * @else
 * @brief 读取并处理所有ADC传感器(轮询模式)
 * @param hadc - 指向ADC句柄结构的指针 
 * @result 所有传感器数据更新为最新的ADC读数 
 * @endif 
 */
void ReadAndProcessAllSensors(ADC_HandleTypeDef* hadc);
#elif ADC_READ_MODE == 1

/**
 * @if Eng 
 * @brief Reads and processes all ADC sensors (DMA mode)
 * @param dmaBuffer - Buffer containing DMA-transferred ADC data
 * @result All sensor data is updated with processed DMA readings 
 * @else 
 * @brief 读取并处理所有ADC传感器(DMA模式)
 * @param dmaBuffer - 包含DMA传输的ADC数据的缓冲区
 * @result 所有传感器数据更新为处理后的DMA读数 
 * @endif 
 */
void ReadAndProcessAllSensors_DMA(uint16_t dmaBuffer[]);
#elif ADC_READ_MODE == 2

/**
 * @if Eng 
 * @brief Reads and processes all ADC sensors (hybrid mode)
 * @param dmaBuffer - Buffer containing DMA-transferred ADC data
 * @param ADC_NUM - Number of ADC channels to process
 * @result All specified sensor data is updated with processed readings
 * @else
 * @brief 读取并处理所有ADC传感器(混合模式)
 * @param dmaBuffer - 包含DMA传输的ADC数据的缓冲区 
 * @param ADC_NUM - 要处理的ADC通道数量 
 * @result 所有指定的传感器数据更新为处理后的读数
 * @endif 
 */
void ReadAndProcessAllSensors(uint16_t dmaBuffer[],uint16_t ADC_NUM);
#endif

/**
 * @if Eng
 * @brief Sends all sensor data via UART 
 * @param huart - Pointer to UART handle structure
 * @result Sensor data is formatted and transmitted via UART 
 * @else 
 * @brief 通过UART发送所有传感器数据 
 * @param huart - 指向UART句柄结构的指针
 * @result 传感器数据被格式化并通过UART传输 
 * @endif 
 */
void sensor_send_data(UART_HandleTypeDef *huart);

/**
 * @if Eng 
 * @brief Processes and transmits ADC data via UART
 * @param huart - Pointer to UART handle structure 
 * @result ADC data is processed and sent via UART
 * @else
 * @brief 处理并通过UART传输ADC数据
 * @param huart - 指向UART句柄结构的指针 
 * @result ADC数据被处理并通过UART发送 
 * @endif 
 */
void ADC_DATA_Dispose(UART_HandleTypeDef *huart);

/**
 * @if Eng
 * @brief Processes and transmits randomized ADC data via UART (for testing)
 * @param huart - Pointer to UART handle structure
 * @result Randomized ADC data is generated and sent via UART
 * @else
 * @brief 处理并通过UART传输随机化的ADC数据(用于测试)
 * @param huart - 指向UART句柄结构的指针
 * @result 生成随机化的ADC数据并通过UART发送 
 * @endif 
 */
void ADC_DATA_Dispose_random(UART_HandleTypeDef *huart);

/**
 * @if Eng 
 * @brief Generates random number within specified range
 * @param target - Center value of the range
 * @param range - ± range around the target value
 * @param decimal_places - Number of decimal places for the result
 * @return Random value within [target-range, target+range] with specified precision 
 * @else 
 * @brief 在指定范围内生成随机数
 * @param target - 范围的中心值
 * @param range - 围绕目标值的±范围
 * @param decimal_places - 结果的小数位数
 * @return 在[target-range, target+range]范围内的随机值，具有指定精度 
 * @endif 
 */
double random_in_range(double target, double range, int decimal_places);

#define HMI_Clear()                       do { USART_printf(Test_huart, "t0.txt=\"\"\xFF\xFF\xFF"); } while (0);
#define HMI_Begin(__order__)              do { USART_printf(Test_huart, "%s=\"", __order__); } while (0);
#define HMI_Begin_Plus(__order__)         do { USART_printf(Test_huart, "%s=%s+\"", __order__, __order__); } while (0);
#define HMI_End()                         do { USART_printf(Test_huart, "\"\xFF\xFF\xFF"); } while (0);

#endif
